package com.gcxy.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.conf.result.R;
import com.gcxy.conf.result.ResultCodeEnum;
import com.gcxy.dto.PenDTO;
import com.gcxy.entity.Pen;
import com.gcxy.entity.Ranch;
import com.gcxy.service.PenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiang
 * @since 2023-12-12
 */
@Slf4j
@RestController
@RequestMapping("/pen")
@Api(tags = "圈舍管理", description = "圈舍管理")
public class PenController {

    @Autowired
    private PenService penService ;


    @PostMapping("/add")
    @ApiOperation("新增圈舍")
    public R add(@RequestBody @Valid Pen pen){
        boolean result = penService.addPen(pen);
        if(result){
            return R.Success(ResultCodeEnum.SUCCESS.getCode() , "新增成功");
        }else{
            return R.Failed("同一牧场下圈舍名称不能重复");
        }
    }

    @PutMapping("update")
    @ApiOperation("修改圈舍")
    public R update(@RequestBody @Valid Pen pen){
        boolean result = penService.updatePen(pen);
        if(result){
            return R.Success(ResultCodeEnum.SUCCESS.getCode() , "修改成功");
        }else{
            return R.Failed(ResultCodeEnum.REPEATED_ADD_PEN.getCode(), "同一牧场下圈舍名称不能重复");
        }
    }

    /**
     * 分页查询
     * @param penDTO
     * @return
     */
    @PostMapping("getPenByPage")
    @ApiOperation("模糊查询圈舍")
    public R<Page<PenDTO>> getPenByPage(@RequestBody PenDTO penDTO){
        Page<PenDTO> penByPage = penService.getPenByPage(penDTO);
        return R.Success(penByPage);
    }

    /**
     * 启用禁用
     * @param pen
     * @return
     */
    @ApiOperation("启用禁用圈舍")
    @PutMapping ("/updateIsEnable")
    public R updateIsEnable(@RequestBody Pen pen){
        penService.updateIsEnable(pen);
        return R.Success();
    }

    @ApiOperation("获取全部信息")
    @GetMapping("getAll")
    public R getAll(){
        return R.Success(penService.list());
    }


    /**
     * 获取圈舍名称
     * @return
     */
    @ApiOperation("获取圈舍名称列表")
    @GetMapping("getPenNameList")
    public R<List<String>> getPenNameList(){
        List<String> penNameList = penService.getPenNameList();
        if(penNameList == null){
            return R.Failed("查询失败");
        }
        return R.Success(penNameList);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("getPenById/{id}")
    @ApiOperation("根据id查询")
    public R getPenById(@PathVariable("id") Integer id){
        Pen pen = penService.getById(id);
        if(pen == null){
            return R.Failed("查询失败");
        }
        return R.Success(pen);
    }
}

